package primetest.impl.service;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;


import javax.inject.Inject;

import primetest.dto.BedDto;
import primetest.impl.dao.BedDao;
import primetest.impl.dao.Transaction;
import primetest.impl.service.BedService;


public class BedService implements Serializable {


	/**
	 *
	 */
	private static final long serialVersionUID = -2631043723576104141L;
	private Transaction transaction;
	private BedDao bedDao;

	@Inject
	public BedService(Transaction transaction, BedDao bedDao){
		this.transaction = transaction;
		this.bedDao = bedDao;
	}

	public List<BedDto> getBedList() throws IOException {
		try{
			transaction.begin();
			List<BedDto> bedList = bedDao.getAllList(transaction);
			//transaction.commit();
			return bedList;
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}



	public void addBed(BedDto bedDto) throws IOException {
		try{
			transaction.begin();
			bedDao.addBed(transaction, bedDto);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void updateBed(BedDto bedDto) throws IOException {
		try{
			transaction.begin();
			bedDao.updateBed(transaction, bedDto);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void deleteBed(String bedSeq) throws IOException {
		try{
			transaction.begin();
			bedDao.deleteBed(transaction, bedSeq);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}

	public BedDto findBed(String bedSeq) throws IOException {
		try{
			transaction.begin();
			return bedDao.findBed(transaction, bedSeq);
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}
}
